package com.example.studentdemo.controller;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class studentArray {
     student[] array = new student[100];
     int studentCount = 0;
     Scanner sc = new Scanner(System.in);

    // 学生信息菜单
    public void menu() {
        while (true) {
            System.out.println("[1] 添加学生信息");
            System.out.println("[2] 删除学生信息");
            System.out.println("[3] 修改学生信息");
            System.out.println("[4] 查找学生信息");
            System.out.println("[5] 显示学生信息");
            System.out.println("[6] 写入文件");
            System.out.println("[0] 退出系统");
            System.out.print("请选择：");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    foundStudent();
                    break;
                case 5:
                    disStudent();
                    break;
                case 6:
                    writeStudent();
                    break;
                case 0:
                    System.out.println("退出系统！");
                    return;
                default:
                    System.out.println("输入错误！");
                    break;
            }
        }
    }


     //添加学生信息
     public void addStudent() {
        try {
            if (studentCount >= array.length) {
                System.out.println("学生信息库已满，无法继续添加！");
                return;
            }

            System.out.println("请输入学生姓名：");
            String name = sc.next();
            System.out.println("请输入学生年龄：");
            int age = sc.nextInt();
            System.out.println("请输入学生班级：");
            String grade = sc.next();
            int id;
            while (true) {
                System.out.println("请输入学生学号：");
                id = sc.nextInt();


                boolean exists = false;
                for (int i = 0; i < studentCount; i++) {
                    if (array[i].getId() == id) {
                        exists = true;
                        break;
                    }
                }

                if (exists) {
                    System.out.println("已存在此学号！请重新输入！");
                } else {
                    break;
                }
            }



            array[studentCount] = new student(id, name, age, grade);
            studentCount++;
            System.out.println("学生信息已添加成功！");
        } catch (Exception e) {
            System.out.println("出现错误了：" + e.getMessage());
        }
    }

    // 删除学生信息
    public void deleteStudent() {
        try {
            if (studentCount == 0) {
                System.out.println("学生信息库为空，无法删除！");
                return;
            }
            System.out.println("请输入要删除的学生学号：");
            int deid = sc.nextInt();
            boolean found = false;
            for (int i = 0; i < studentCount; i++) {
                if (array[i].getId() == deid) {
                    found = true;
                    // 删除操作
                    for (int j = i; j < studentCount - 1; j++) {
                        array[j] = array[j + 1];
                    }
                    array[studentCount - 1] = null; // 清空最后一个位置
                    studentCount--; // 学生数量自减
                    System.out.println("学号为" + deid + "的学生信息已经删除！");
                    break;
                }
            }
            if (!found) {
                System.out.println("没有找到学号为" + deid + "的学生信息！");
            }
        } catch (Exception e) {
            System.out.println("出现错误了：" + e.getMessage());
        }
    }

    // 修改学生信息
    public void updateStudent() {
        try {
            if (studentCount == 0) {
                System.out.println("学生信息库中没有学生信息！");
                return;
            }

            System.out.println("请输入你要修改的学生学号：");
            int id = sc.nextInt();
            boolean found = false;
            for (int i = 0; i < studentCount; i++) {
                if (array[i].getId() == id) {
                    while (true) {
                        System.out.println("\n请选择你要修改的类型：");
                        System.out.println("[1] 学生姓名");
                        System.out.println("[2] 学生年龄");
                        System.out.println("[3] 学生学号");
                        System.out.println("[4] 学生班级");
                        System.out.println("[0] 退出修改菜单");
                        System.out.print("请选择：");
                        int choice = sc.nextInt();
                        switch (choice) {
                            case 1:
                                System.out.println("请输入新的学生姓名：");
                                String upname = sc.next();
                                array[i].setName(upname);
                                System.out.println("学生姓名修改成功！");
                                break;
                            case 2:
                                System.out.println("请输入新的学生年龄：");
                                int upage = sc.nextInt();
                                array[i].setAge(upage);
                                System.out.println("学生年龄修改成功！");
                                break;
                            case 3:
                                System.out.println("请输入新的学生学号：");
                                int upid = sc.nextInt();
                                array[i].setId(upid);
                                System.out.println("学生学号修改成功！");
                                break;
                            case 4:
                                System.out.println("请输入新的学生班级：");
                                String upgrade = sc.next();
                                array[i].setGrade(upgrade);
                                System.out.println("学生班级修改成功！");
                                break;
                            case 0:
                                System.out.println("退出修改菜单！");
                                return;
                            default:
                                System.out.println("输入无效！");
                                break;
                        }
                    }
                }
            }
            if (!found) {
                System.out.println("没有找到学号为" + id + "的学生！");
            }
        } catch (Exception e) {
            System.out.println("出现错误了：" + e.getMessage());
        }
    }

    // 查找学生信息
    public void foundStudent() {
        try {
            if (studentCount == 0) {
                System.out.println("学生信息库中没有学生信息！");
                return;
            }

            System.out.println("请输入学生的学号：");
            int disid = sc.nextInt();
            boolean found = false;
            System.out.println("学生信息如下：");
            for (int i = 0; i < studentCount; i++) {
                if (array[i].getId() == disid) {
                    found = true;
                    System.out.println(array[i]);
                }
            }
            if (!found) {
                System.out.println("没有找到学号为" + disid + "的学生！");
            }
        } catch (Exception e) {
            System.out.println("出现错误了：" + e.getMessage());
        }
    }


    //显示学生信息
    public void disStudent(){
         if (studentCount==0){
             System.out.println("学生信息库中没有学生信息！");
             return;
         }
        System.out.println("学生信息如下：");
         for (int i=0;i<studentCount;i++){
             System.out.println(array[i]);
         }

    }

    //将学生信息写入文件
    public void writeStudent(){
        try {
            if (studentCount==0){
                System.out.println("显示库里没有信息！");
            }
            BufferedWriter bu=new BufferedWriter(new FileWriter("student.txt"));
            for (int i=0;i<studentCount;i++) {
                bu.write(array[i].toString());//将数组中的信息写入文件
                bu.newLine();//换行

            }
            System.out.println("学生信息写入成功！");
            bu.close();//关闭文件，以确保信息能写入文件
        } catch (IOException e) {
            System.out.println("出现错误了：" + e.getMessage());
        }
    }
}